import switchTab from "../commonComponents/switchTab.vue"
import headerWrap from "../commonComponents/header.vue"
import actionSheet from "../commonComponents/actionSheet.vue"

export default {
  data() {
    return {
      switchFlag: 0,
      tabData: ['已上报', '草稿箱'],
      submitList: [],
      draftList: [],
      submitPageIndex: [],
      draftPageIndex: [],
      temList: [],
      actionData: []
    }
  },
  methods: {
    init() {
      this.getTem();
      setTimeout(() => {
        this.submitListInit();
        this.draftListInit();
      }, 40);
      _methods.headerTitleChange(1);

    },
    submitListInit() {
      let self = this;
      _data.submitList = _methods.initMeScroll("submitList", {
        //刷新
        down: {
          callback: () => {
            self.getList(1, 1);
          }
        },
        //加载更多
        up: {
          warpId: "submitList",
          callback: () => {
            self.getList(1, 2);
          }
        }
      });
    },
    draftListInit() {
      _data.draftList = _methods.initMeScroll("draftList", {
        //刷新
        down: {
          callback: () => {
            this.getList(2, 1);
          }
        },
        //加载更多
        up: {
          warpId: "draftList",
          callback: () => {
            this.getList(2, 2);
          }
        }
      });
    },
    getList(listFlag, scrollFlag) {
      /*
       listFlag：1：已上报 2：草稿箱
       scrollFlag：1：刷新 2：加载更多
       */
      //当前第几页key
      let pageKey = ["", "submitPageIndex", "draftPageIndex"][listFlag];
      //列表参数
      let listKey = ["", "submitList", "draftList"][listFlag];
      // //是否为已上报

      let isEnabled = ["", 1, 4][listFlag];
      // ajax data
      let data = {
        size:_data.pageSize,
        templateID:"",
        isEnabled,
      };
      //刷新 加载 当前第几页处理
      if (scrollFlag === 1) {
        this[pageKey] = 1;
      } else {
        this[pageKey] = this[pageKey] - 0 + 1;
      }
      data.page = this[pageKey];
      //ajax获取数据
      _ajax.noLoading(_ajaxUrls.addInfoList.url, data,
        (ajData) => {
          let lt = ajData.result.records;
          let len = lt.length;
          let fn = (vu = []) => {
            let arr = vu;
            lt.forEach((i) => {
              i.time = i.createTime;
              i.typeName = i.templateName;
              arr.push(i);
            });
            return arr;
          };
          if (scrollFlag === 1) {
            //刷新
            this[listKey] = fn();
          } else {
            //加载更多
            this[listKey] = fn(this[listKey]);
          }
          _data[listKey].endSuccess(len, len == _data.pageSize);
        },
        _ajaxUrls.addInfoList.type);
    },
    goDetails(item) {
      _methods.goPath(`/infoSubmitList/infoDetail?id=${item.id}&type=1`);
    },
    switchClick(i) {
      this.switchFlag = i;
    },
    goSubmit(item,isPush = 0,isDraft = 0) {
      let infoId = isDraft ? item.id:0;
      _methods.goPath(`/infoSubmitList/submit?id=${item.templateID}&isDraft=${isDraft}&isPush=${isPush}&infoId=${infoId}`);
    },
    _addTemShow() {
      let picker = new mui.PopPicker();
      picker.setData(this.temList);
      picker.show( (selectItems) => {
        let si = selectItems[0];
        si.templateID = si.id;
        this.goSubmit(si);
      });
    },
    getTem(){
      let data = {
        templateID:"",
        formType:1,
        page:1,
        size:999,
      };
      _ajax.noLoading(_ajaxUrls.addTemList.url,
        data,
        (ajData) => {
          this.temList = ajData.result.map(i=>{
            i.text = i.name;
            i.value = i.formType;
            return i;
          });
        },
        _ajaxUrls.addTemList.type);
    },
  },
  components: {
    switchTab,
    headerWrap,
    actionSheet,
  },
  mounted() {
    window._vrvInit.isPush().then(() => {
      this.init();
    });
    _methods.headerRightShow({val: 999, txt: ""});
    // this.init();
    _data.busInit.on_(() => {
      this._addTemShow();
    });
  },
  filters: {
    typeF(v) {
      return {
        1: "信息",
        2: "预警",
        3: "事件",
      }[parseInt(v)]
    }
  },
  beforeDestroy() {
    _data.busInit.off_();
    _methods.headerRightShow();
  }
}
